<!--
 * @Description:
 * @version:
 * @Author: sunqian_sr
 * @Date: 2022-12-02 18:59:00
 * @LastEditors: Andy
 * @LastEditTime: 2023-03-01 11:13:18
-->
<template>
  <x-dialog :title="title" :show.sync="visible" v-if="visible" :before-close="handleClose" width="35%">
    <el-form :model="params" class="demo-form-inline" ref="params" :rules="rules" label-position="top">
      <el-row :gutter="20">
        <el-col :span="24" v-if="flag">
          <el-form-item :label="$t('organization.organiz.pre')" prop="parentId">
            <x-select-tree
              v-model="params.parentId"
              ref="selectTree"
              :options="orgTreeData"
              :placeholder="$t('system.user.pleaseSelect')"
              :defaultProps="defaultOrgProps"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('organization.organiz.name')" prop="orgName">
            <el-input v-model="params.orgName" :placeholder="$t('system.user.remarktxt')" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('organization.organiz.code')" prop="orgCode">
            <el-input v-model="params.orgCode" :placeholder="$t('system.user.remarktxt')" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('organization.organiz.shoreName')" prop="shortName">
            <el-input v-model="params.shortName" :placeholder="$t('system.user.remarktxt')" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('organization.organiz.type')" prop="orgType">
            <x-select
              :placeholder="$t('system.user.pleaseSelect')"
              v-model="params.orgType"
              :options="dictOptions['upms_org.org_type']"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('organization.post.toStart')" prop="state">
            <x-radio-group :list-data="dictOptions['system.state']" v-model="params.state"></x-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer">
      <el-button @click="handleClose"> {{ $t('common.cancel') }} </el-button>
      <el-button
        type="primary"
        @click="
          loading = true
          handleSubmit()
        "
        :loading="loading"
      >
        {{ $t('common.confirm') }}
      </el-button>

      <!-- <el-button
        v-if="'parentId' in params && params.parentId + '' === '0' && params?.id"
        type="danger"
        @click="handleDeleteOrg"
      >
        {{ $t('common.delete') }}
      </el-button> -->
    </span>
  </x-dialog>
</template>
<script>
  import { cloneDeep, omit, throttle } from '@package/utils'
  import { apiGetOrgInfo, apiPostEditOrganiz, apiPostAddOrganiz } from '../api'
  import { EnumApiCode } from '../../../../../../globalEnum.js'

  export default {
    props: {
      orgTreeData: {
        type: Array,
        default: () => []
      },
      defaultOrgProps: {
        type: Object,
        default: () => ({})
      },
      dictOptions: {
        type: Object,
        default: () => ({})
      }
    },
    data() {
      return {
        visible: false,
        loading: false,
        flag: true,
        params: {
          parentId: '0',
          orgName: '',
          shortName: '',
          orgCode: '',
          orgType: '',
          state: '1'
        },
        rules: {
          parentId: [{ required: true, message: this.$t('organization.organiz.prePlaceholder'), trigger: 'blur' }],
          orgName: [
            { required: true, message: this.$t('organization.organiz.namePlaceholder'), trigger: 'blur' },
            { min: 1, max: 30, message: this.$t('organization.organiz.inputLength'), trigger: 'blur' }
          ],
          shortName: [
            { required: true, message: this.$t('organization.organiz.shortNamePlaceHolder'), trigger: 'blur' },
            { min: 1, max: 30, message: this.$t('organization.organiz.inputLength'), trigger: 'blur' }
          ],
          orgCode: [
            { required: true, message: this.$t('organization.organiz.codePlaceholder'), trigger: 'blur' },
            { min: 1, max: 30, message: this.$t('organization.organiz.inputLength'), trigger: 'blur' }
          ],
          orgType: [{ required: true, message: this.$t('organization.organiz.typePlaceHolder'), trigger: 'blur' }],
          state: [{ required: true, message: this.$t('organization.organiz.statePlaceholder'), trigger: 'blur' }]
        }
      }
    },
    methods: {
      handleOpen(info, flag = true) {
        this.visible = true
        this.params.parentId = info?.parentId ?? 0
        this.flag = flag && `${info.parentId}` !== '0'
        if (info?.id) {
          this.title = this.$t('organization.organiz.editOrg')
          this.getOrgInfo(info.id)
          return
        }
        this.title = this.$t('organization.organiz.addOrg')
      },
      async getOrgInfo(id) {
        const { data, code } = await apiGetOrgInfo(id)
        if (code === EnumApiCode.Success) {
          this.$nextTick(() => {
            this.params = Object.assign(this.params, cloneDeep(data))
            this.params.orgType += ''
            this.params.state += ''
          })
        }
      },
      handleSubmit: throttle(function () {
        this.loading = true
        this.$refs.params.validate(async (valid) => {
          if (valid) {
            const param = {
              ...this.params,
              parentId: typeof this.params.parentId === 'object' ? this.params.parentId.value : this.params.parentId
            }
            try {
              const { code, msg } =
                'id' in this.params ? await apiPostEditOrganiz(param) : await apiPostAddOrganiz(param)
              this.loading = false
              if (code === EnumApiCode.Success) {
                this.$message.success(param.id ? this.$t('common.editSuccess') : this.$t('common.newSuccess'))
                this.$emit('handleSuccess')
                this.handleClose()
              } else {
                this.$message.error(msg)
              }
            } finally {
              this.loading = false
            }
          } else {
            this.loading = false
            return false
          }
        })
      }, 1000),
      handleClose() {
        this.$refs.params.resetFields()
        this.params = omit(this.params, 'id')
        this.loading = false
        this.visible = false
      },
      handleDeleteOrg() {
        this.$emit('on-delete', this.params.id)
        this.handleClose()
      }
    }
  }
</script>
